package com.digiwin.muke;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * @Author yanggld
 * @Date 2021/04/25-15:05
 */
public class StreamingWCJavaApp {
    public static void main(String[] args) throws Exception {
        // 获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 读取数据
        DataStreamSource<String> text = env.socketTextStream("c1", 9999);
        // 转换方式一
//        text.flatMap(new FlatMapFunction<String, WC>() {
//            @Override
//            public void flatMap(String value, Collector<WC> collector) throws Exception {
//                String[] tokens = value.toLowerCase().split(",");
//                for (String token : tokens) {
//                    if (token.length() > 0) {
//                        collector.collect(new WC(token,1));
//                    }
//                }
//            }
//        }).keyBy("word")
//        .timeWindow(Time.seconds(5))
//        .sum("count").print()
//        .setParallelism(1);

        // 转换方式二
        text.flatMap(new MyFlatMap()).keyBy("word")
        .timeWindow(Time.seconds(5))
        .sum("count").print()
        .setParallelism(1);

        env.execute("StreamingWCJavaApp");
    }

    public static class MyFlatMap implements FlatMapFunction<String,WC>{
        @Override
        public void flatMap(String value, Collector<WC> collector) throws Exception {
            String[] tokens = value.toLowerCase().split(",");
            for (String token : tokens) {
                if (token.length() > 0) {
                    collector.collect(new WC(token,1));
                }
            }
        }
    }
}
